package com.pcafs.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.pcafs.common.core.domain.entity.SysDept;
import com.pcafs.manage.domain.ActUser;
import com.pcafs.manage.mapper.ActUserMapper;
import com.pcafs.manage.vo.TDetail;
import com.pcafs.system.mapper.SysDeptMapper;
import com.pcafs.manage.bo.TeacherInfoAddBo;
import com.pcafs.manage.bo.TeacherInfoEditBo;
import com.pcafs.manage.bo.TeacherInfoQueryBo;
import com.pcafs.manage.vo.TeacherInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.pcafs.manage.domain.TeacherInfo;
import com.pcafs.manage.mapper.TeacherInfoMapper;
import com.pcafs.manage.service.ITeacherInfoService;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 教师信息Service业务层处理
 *
 * @author pcafs
 * @date 2021-05-04
 */
@Service
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper, TeacherInfo> implements ITeacherInfoService {

    @Autowired
    private TeacherInfoMapper teacherInfoMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ActUserMapper actUserMapper;


    @Override
    public TeacherInfoVo queryById(Long id){
        TeacherInfo db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, TeacherInfoVo.class);
    }

    @Override
    public List<TeacherInfoVo> queryList(TeacherInfoQueryBo bo) {
        LambdaQueryWrapper<TeacherInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAccount() != null, TeacherInfo::getAccount, bo.getAccount());
        lqw.eq(StrUtil.isNotBlank(bo.getLocation()), TeacherInfo::getLocation, bo.getLocation());
        lqw.eq(StrUtil.isNotBlank(bo.getNativePlace()), TeacherInfo::getNativePlace, bo.getNativePlace());
        lqw.eq(StrUtil.isNotBlank(bo.getPolitical()), TeacherInfo::getPolitical, bo.getPolitical());
        lqw.eq(bo.getTeacherTime() != null, TeacherInfo::getTeacherTime, bo.getTeacherTime());
        lqw.eq(StrUtil.isNotBlank(bo.getGraduationSchool()), TeacherInfo::getGraduationSchool, bo.getGraduationSchool());
        lqw.eq(bo.getBirthday() != null, TeacherInfo::getBirthday, bo.getBirthday());
        lqw.eq(bo.getDeptId() != null, TeacherInfo::getDeptId, bo.getDeptId());
        lqw.eq(StrUtil.isNotBlank(bo.getEmail()), TeacherInfo::getEmail, bo.getEmail());
        lqw.eq(StrUtil.isNotBlank(bo.getPhonenumber()), TeacherInfo::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StrUtil.isNotBlank(bo.getSex()), TeacherInfo::getSex, bo.getSex());
        lqw.eq(StrUtil.isNotBlank(bo.getAvatar()), TeacherInfo::getAvatar, bo.getAvatar());
        lqw.eq(StrUtil.isNotBlank(bo.getTname()), TeacherInfo::getTname, bo.getTname());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<TeacherInfoVo> entity2Vo(Collection<TeacherInfo> collection) {
        List<TeacherInfoVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TeacherInfoVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TeacherInfo> page = (Page<TeacherInfo>)collection;
            Page<TeacherInfoVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TeacherInfoAddBo bo) {
        TeacherInfo add = BeanUtil.toBean(bo, TeacherInfo.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TeacherInfoEditBo bo) {
        TeacherInfo update = BeanUtil.toBean(bo, TeacherInfo.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TeacherInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public TDetail selectTDetail(Long id) {

        TDetail tDetail = new TDetail();
        TeacherInfo teacherInfo = teacherInfoMapper.selectById(id);

        List<SysDept> sysDepts = sysDeptMapper.selectParentNode(teacherInfo.getDeptId());
        for (SysDept sysDept : sysDepts) {
            if (StrUtil.equals(sysDept.getDeptType(), "1")) {
                teacherInfo.setCollege(sysDept.getDeptName());
            } else if (StrUtil.equals(sysDept.getDeptType(), "2")) {
                teacherInfo.setInstitute(sysDept.getDeptName());
            } else if (StrUtil.equals(sysDept.getDeptType(), "3")) {
                teacherInfo.setMajor(sysDept.getDeptName());
            }else if (StrUtil.equals(sysDept.getDeptType(), "4")) {
                teacherInfo.setTClass(sysDept.getDeptName());
            }
        }
        tDetail.setTeacherInfo(teacherInfo);

        // 参项信息
        List<ActUser> actUsers = actUserMapper.selectActInfoByTeacher(id);
        if (actUsers.size() != 0) {
            tDetail.setActUserList(actUsers);
        }
        return tDetail;
    }

    @Override
    public List<TeacherInfo> queryListVo(TeacherInfo teacherInfo) {
        return teacherInfoMapper.queryListVo(teacherInfo);
    }
}
